package com.afkt.project.ui.adapter;


import com.afkt.project.model.bean.MyListBean;
import com.ohos.afkt.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class MyListAdapter extends BaseItemProvider {

    private List<MyListBean> mData;
    private AbilitySlice slice;

    public MyListAdapter(List<MyListBean> mData, AbilitySlice slice) {
        this.mData = mData;
        this.slice = slice;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int position, Component component,
                                  ComponentContainer componentContainer) {
        MyListBean myListBean = mData.get(position);
        ListenerViewHolder holder = new ListenerViewHolder();
        if (component == null) {
            component = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_layout_mylist_item,
            null, false);
            holder.listenerName = (Text) component.findComponentById(ResourceTable.Id_listener_item_name);
            holder.listenerName.setText(myListBean.getName());
            component.setTag(holder);
        } else {
            holder = (ListenerViewHolder) component.getTag();
        }
        holder.listenerName.setText(myListBean.getName());
        return component;
    }

    static class ListenerViewHolder {
        Text listenerName;
    }
}
